package com.teamwork.module01.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module01.Academy;

import com.teamwork.common.result.Result;


import com.teamwork.module01.mapper.AcademyMapper;
import com.teamwork.module01.service.AcademyService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;


import java.util.List;


@Service
@Slf4j
public class AcademyServiceImpl extends ServiceImpl<AcademyMapper, Academy> implements AcademyService{
    /**
     **/

    @Override
    public Result getAcdemyByName(String name) {

        LambdaQueryWrapper<Academy> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Academy::getAcademyName,name).or().like(Academy::getAcademyName,name);
        Academy academy = this.getOne(wrapper);





        if( academy==null)
        {
            //log.info("没查到");
            return Result.failure("没有查询到学院信息");
        }
        return Result.success(academy);


    }



    @Override
    public Result getPage(Integer page,Integer size) {

        Page<Academy> pageInfo =new Page<>(page,size);

         this.page(pageInfo);



        return Result.success(pageInfo);
    }

    /**
     * 修改或者添加学院
     * @param academy
     * @return
     */
    @Override
    public Result InsertOrUpdate(Academy academy) {

        LambdaQueryWrapper<Academy> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Academy::getAcademyName,academy.getAcademyName());
       // log.info("{}",academy);
        if(academy.getId()!=null){
            //id不为空，修改学院
            //先查询该学院是否已经存在，
            Academy one = this.getOne(wrapper);
            if(one!=null){

                return Result.failure("该学院名称已经存在，请重新修改！");
            }
            //名称不存在，可以修改
            boolean b = this.updateById(academy);

            if(!b) {
                log.error("修改id为:"+academy.getId()+"的学院信息失败!!!");
                return Result.failure("修改学院信息失败");
            }
                log.info("修改id为:"+academy.getId()+"学院信息...");;
            return Result.success("修改学院信息成功");

        }

            //id为空，添加学院
            //先查询该学院是否存在

            Academy one = this.getOne(wrapper);
            if(one!=null){
                return Result.failure("该学院已经存在!");
            }
            //学院不存在，可以添加
            boolean b = this.save(academy);
            if(!b){
                log.error("添加学院信息失败！！！");
                return Result.failure("添加学院信息失败!");
            }
               log.info("添加学院:"+academy.getAcademyName()  +"成功...");
            return Result.success("添加学院信息成功！");


    }


    /**
     * 批量删除学院
     * @param idlist
     * @return
     */
    @Override
    public Result batchDelete(List<Integer> idlist) {

        for (Integer id  : idlist) {

            boolean b = this.removeById(id);
            if(!b){
                log.error("删除id为:"+id+"的学院信息失败!");
                return Result.failure("删除学院id为"+id+"的学院信息失败");
            }


        }
        return Result.success("删除学院信息成功");
    }




}
